Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bound channel elements processed every time process_events is called #200

Merged
merged 3 commits into from
Jul 21, 2024

Conversation

notgull
Copy link
Member

@notgull notgull commented Jul 21, 2024

At the moment, the channel implentation processes as many channel
elements as possible every time "process_events" is called. However,
in multithreaded cases this can cause the loop to be stuck in the
"process_events" section of a channel forever. If one thread keeps
sending new elements into the channel while the current thread keeps
reading them, it will starve other event sources of running time.

This commit fixes this issue by bounding the number of channel elements
that can be processed every time "process_events" is called. It chooses
the smallest of the following numbers:

  • The capacity of the channel.
  • 1024 (chosen because this is also used by async-executor)

If the channel is not empty after we have read this number of elements,
the underlying source is not triggered. This should make it so the
channel is immediately re-polled on the next dispatch. However it gives
other sources more time to run.

Copy link

codecov bot commented Jul 21, 2024

Codecov Report

Attention: Patch coverage is 95.18072% with 4 lines in your changes missing coverage. Please review.

Project coverage is 85.44%. Comparing base (a87d1f6) to head (d09724a).

Files Patch % Lines
src/sources/futures.rs 89.74% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #200      +/-   ##
==========================================
+ Coverage   85.08%   85.44%   +0.36%     
==========================================
  Files          13       13              
  Lines        1837     1876      +39     
==========================================
+ Hits         1563     1603      +40     
+ Misses        274      273       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@notgull
Copy link
Member Author

notgull commented Jul 21, 2024

It appears this has exposed a bug in Windows' Ping implementation. Standby while I look into this.

notgull added 3 commits July 21, 2024 09:16
At the moment, the channel implentation processes as many channel
elements as possible every time "process_events" is called. However,
in multithreaded cases this can cause the loop to be stuck in the
"process_events" section of a channel forever. If one thread keeps
sending new elements into the channel while the current thread keeps
reading them, it will starve other event sources of running time.

This commit fixes this issue by bounding the number of channel elements
that can be processed every time "process_events" is called. It chooses
the smallest of the following numbers:

- The capacity of the channel.
- 1024 (chosen because this is also used by async-executor)

If the channel is not empty after we have read this number of elements,
the underlying source is not triggered. This should make it so the
channel is immediately re-polled on the next dispatch. However it gives
other sources more time to run.

Signed-off-by: John Nunley <[email protected]>
Previously, we only processed the source in the channel/executor
implementations when we needed to clear the readiness; however this
causes issues in the Windows `Ping` implementation. As a workaround,
this commit always calls "process_events" in `Ping`, but simply
re-notifies the PingSource if we need to try again.

Signed-off-by: John Nunley <[email protected]>
@notgull notgull merged commit 86c6713 into master Jul 21, 2024
14 checks passed
@notgull notgull deleted the bounded branch July 21, 2024 16:46
@notgull notgull mentioned this pull request Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant